package com.pss.mq.util;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodeUtils {
	private static String url = "";
	private static String account = "";
	private static String password = "";
	private static String sign = "";
	private static String subcode = "";
	private static String template = "";
	private static String templateSucccess = "";
	private static Properties prop = null;

	static {
		try {
			prop =  new Properties();
			InputStream in = new CodeUtils().getClass().getClassLoader().getResourceAsStream("sms.properties");
			prop.load(in);
			in.close();

			url = prop.getProperty("sms.url");
			account = prop.getProperty("sms.account");
			password = prop.getProperty("sms.password");
			sign = prop.getProperty("sms.sign");
			subcode = prop.getProperty("sms.subcode");
			template = prop.getProperty("sms.template.verifyno");
			templateSucccess = prop.getProperty("sms.template.success");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String generateVerifyNo(){
		StringBuffer verifyNo = new StringBuffer();
		Random random = new Random();
		String[] code = {"0","1","2","3","4","5","6","7","8","9"};
		for (int j = 0; j < 6; j++) {
			verifyNo.append(code[random.nextInt(10)]);
		}
		return verifyNo.toString();
		
	}

	public static String generateCode(){
		StringBuffer buffer = new StringBuffer();
		Random random = new Random();
		String[] code = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
		for (int j = 0; j < 5; j++) {
			buffer.append(code[random.nextInt(26)]);
		}
		return buffer.toString();

	}

	//审核结果通知
//	public static String sendApproveSms(VerifyNo verifyNo) throws URIException {
//		String content = verifyNo.getVerifyNo();
//		JSONHttpClient jsonHttpClient = new JSONHttpClient(url);
//		jsonHttpClient.setRetryCount(1);
//		String sendhRes = jsonHttpClient.sendSms(account, password, verifyNo.getPhoneNo(), content, sign, subcode);
//		return sendhRes;
//	}

	public static boolean isMobile(String phoneNo){
		Pattern p = Pattern.compile("^1(3|4|5|7|8)\\d{9}$");
		Matcher m = p.matcher(phoneNo.trim());
		return m.matches();
	}

}
